package com.xbongbong.paas.enums;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * Description: 流程中saas应用筛选项
 * @author 李杰
 * @date 2019/1/24 10:43
 * @version v1.0
 * @since v1.0
 */
public enum ProcessSaasFormEnum {
    /**
     * 客户
     */
    CUSTOMER(ProcessSaasAppEnum.CRM.getType(), XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getAlias(), "enum.xbbRefType.name.customer_management", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 合同
     */
    CONTRACT(ProcessSaasAppEnum.CRM.getType(), XbbRefTypeEnum.CONTRACT.getCode(), XbbRefTypeEnum.CONTRACT.getAlias(), "enum.xbbRefType.name.contract", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 销售机会
     */
    SALES_OPPORTUNITY(ProcessSaasAppEnum.CRM.getType(), XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), XbbRefTypeEnum.SALES_OPPORTUNITY.getAlias(), "enum.xbbRefType.name.sales_opportunity", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 回款单
     */
    PAYMENT_SHEET(ProcessSaasAppEnum.FUND_MANAGEMENT.getType(), XbbRefTypeEnum.PAYMENT_SHEET.getCode(), XbbRefTypeEnum.PAYMENT_SHEET.getAlias(), "enum.xbbRefType.name.payment_sheet", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 发票
     */
    INVOICE(ProcessSaasAppEnum.FUND_MANAGEMENT.getType(), XbbRefTypeEnum.INVOICE.getCode(), XbbRefTypeEnum.INVOICE.getAlias(), "enum.fundManagementMenu.name.invoice", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 退货退款
     */
    REFUND(ProcessSaasAppEnum.CRM.getType(), XbbRefTypeEnum.REFUND.getCode(), XbbRefTypeEnum.REFUND.getAlias(), "enum.xbbRefType.name.refund", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 报价单
     */
    QUOTATION(ProcessSaasAppEnum.CRM.getType(), XbbRefTypeEnum.QUOTATION.getCode(), XbbRefTypeEnum.QUOTATION.getAlias(), "enum.xbbRefType.name.quotation", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 工单
     */
    WORK_ORDER_CENTER(ProcessSaasAppEnum.WORK_ORDER.getType(), XbbRefTypeEnum.WORK_ORDER.getCode(), XbbRefTypeEnum.WORK_ORDER.getAlias(), "enum.xbbRefType.name.work_order", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 采购合同
     */
    PURCHASE(ProcessSaasAppEnum.JXC.getType(), XbbRefTypeEnum.PURCHASE.getCode(), XbbRefTypeEnum.PURCHASE.getAlias(), "enum.xbbRefType.name.purchase", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 采购退款
     */
    RETURNED_PURCHASE(ProcessSaasAppEnum.JXC.getType(), XbbRefTypeEnum.RETURNED_PURCHASE.getCode(), XbbRefTypeEnum.RETURNED_PURCHASE.getAlias(), "enum.xbbRefType.name.returned_purchase", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 盘点单
     */
    INVENTORY(ProcessSaasAppEnum.JXC.getType(), XbbRefTypeEnum.INVENTORY.getCode(), XbbRefTypeEnum.INVENTORY.getAlias(), "enum.xbbRefType.name.inventory", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 调拨单
     */
    TRANSFER(ProcessSaasAppEnum.JXC.getType(), XbbRefTypeEnum.TRANSFER.getCode(), XbbRefTypeEnum.TRANSFER.getAlias(), "enum.xbbRefType.name.transfer", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 其他出库单
     */
    OTHER_OUTSTOCK(ProcessSaasAppEnum.JXC.getType(), XbbRefTypeEnum.OTHER_OUTSTOCK.getCode(), XbbRefTypeEnum.OTHER_OUTSTOCK.getAlias(), "enum.xbbRefType.name.other_outstock", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 销售出库单
     */
    CONTRACT_OUTSTOCK(ProcessSaasAppEnum.JXC.getType(), XbbRefTypeEnum.CONTRACT_OUTSTOCK.getCode(), XbbRefTypeEnum.CONTRACT_OUTSTOCK.getAlias(), "enum.xbbRefType.name.contract_outstock", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 采购退货出库单
     */
    RETURNED_PURCHASE_OUTSTOCK(ProcessSaasAppEnum.JXC.getType(), XbbRefTypeEnum.RETURNED_PURCHASE_OUTSTOCK.getCode(), XbbRefTypeEnum.RETURNED_PURCHASE_OUTSTOCK.getAlias(), "enum.xbbRefType.name.returned_purchase_outstock", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 生产领料单
     */
    PRODUCTION_MATERIEL_OUTSTOCK(ProcessSaasAppEnum.JXC.getType(), XbbRefTypeEnum.PRODUCTION_MATERIEL_OUTSTOCK.getCode(), XbbRefTypeEnum.PRODUCTION_MATERIEL_OUTSTOCK.getAlias(), "enum.xbbRefType.name.production_materiel_outstock", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 工单出库
     */
    WORK_ORDER_OUTSTOCK(ProcessSaasAppEnum.JXC.getType(), XbbRefTypeEnum.WORK_ORDER_OUTSTOCK.getCode(), XbbRefTypeEnum.WORK_ORDER_OUTSTOCK.getAlias(), "enum.xbbRefType.name.work_order_outstock", DistributorMarkEnum.OTHER.getCode()),
    // 采购入库单
    // todo 采购入库单暂时用instock的权限 后期统一改
    PURCHASE_INSTOCK(ProcessSaasAppEnum.JXC.getType(), XbbRefTypeEnum.PURCHASE_INSTOCK.getCode(), XbbRefTypeEnum.PURCHASE_INSTOCK.getAlias(), "enum.xbbRefType.name.purchase_instock", DistributorMarkEnum.OTHER.getCode()),
    // 销售退货入库
    REFUND_INSTOCK(ProcessSaasAppEnum.JXC.getType(), XbbRefTypeEnum.REFUND_INSTOCK.getCode(), XbbRefTypeEnum.REFUND_INSTOCK.getAlias(), "enum.xbbRefType.name.refund_instock", DistributorMarkEnum.OTHER.getCode()),
    // 其他入库单
    OTHER_INSTOCK(ProcessSaasAppEnum.JXC.getType(), XbbRefTypeEnum.OTHER_INSTOCK.getCode(), XbbRefTypeEnum.OTHER_INSTOCK.getAlias(), "enum.xbbRefType.name.other_instock", DistributorMarkEnum.OTHER.getCode()),
    //生产入库单
    PRODUCTION_INSTOCK(ProcessSaasAppEnum.JXC.getType(), XbbRefTypeEnum.PRODUCTION_INSTOCK.getCode(), XbbRefTypeEnum.PRODUCTION_INSTOCK.getAlias(), "enum.xbbRefType.name.production_instock", DistributorMarkEnum.OTHER.getCode()),
    //生产退料单
    RETURNED_MATERIEL_INSTOCK(ProcessSaasAppEnum.JXC.getType(), XbbRefTypeEnum.RETURNED_MATERIEL_INSTOCK.getCode(), XbbRefTypeEnum.RETURNED_MATERIEL_INSTOCK.getAlias(), "enum.xbbRefType.name.returned_materiel_instock", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 付款单
     */
    PAY_SHEET(ProcessSaasAppEnum.FUND_MANAGEMENT.getType(), XbbRefTypeEnum.PAY_SHEET.getCode(), XbbRefTypeEnum.PAY_SHEET.getAlias(), "enum.xbbRefType.name.pay_sheet", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 进项发票
     */
    PURCHASE_INVOICE(ProcessSaasAppEnum.FUND_MANAGEMENT.getType(), XbbRefTypeEnum.PURCHASE_INVOICE.getCode(), XbbRefTypeEnum.PURCHASE_INVOICE.getAlias(), "enum.xbbRefType.name.purchase_invoice", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 装配出入库单
     */
    ASSEMBLE(ProcessSaasAppEnum.JXC.getType(), XbbRefTypeEnum.ASSEMBLE.getCode(), XbbRefTypeEnum.ASSEMBLE.getAlias(), "enum.xbbRefType.name.assemble", DistributorMarkEnum.OTHER.getCode()),
    /**
     * BOM单
     */
    BOM_BILL(ProcessSaasAppEnum.JXC.getType(), XbbRefTypeEnum.BOM_BILL.getCode(), XbbRefTypeEnum.BOM_BILL.getAlias(), "enum.xbbRefType.name.bom_bill", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 生产单
     */
    PRODUCTION_ORDER(ProcessSaasAppEnum.JXC.getType(), XbbRefTypeEnum.PRODUCTION_ORDER.getCode(), XbbRefTypeEnum.PRODUCTION_ORDER.getAlias(), "enum.xbbRefType.name.production_order", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 供应商
     */
    SUPPLIER(ProcessSaasAppEnum.JXC.getType(),XbbRefTypeEnum.SUPPLIER.getCode(),XbbRefTypeEnum.SUPPLIER.getAlias(),"enum.xbbRefType.name.supplier", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 供应商联系人
     */
    SUPPLIER_CONTACT(ProcessSaasAppEnum.JXC.getType(),XbbRefTypeEnum.SUPPLIER_CONTACT.getCode(),XbbRefTypeEnum.SUPPLIER_CONTACT.getAlias(),"enum.xbbRefType.name.supplier_contact", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 联系人
     */
    CONTACT(ProcessSaasAppEnum.CRM.getType(),XbbRefTypeEnum.CONTACT.getCode(),XbbRefTypeEnum.CONTACT.getAlias(),"enum.xbbRefType.name.contact", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 市场活动
     */
    MARKET_ACTIVITY(ProcessSaasAppEnum.MARKET_MANAGEMENT.getType(),XbbRefTypeEnum.MARKET_ACTIVITY.getCode(),XbbRefTypeEnum.MARKET_ACTIVITY.getAlias(),"enum.xbbRefType.name.market_activity", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 线索
     */
    CLUE(ProcessSaasAppEnum.MARKET_MANAGEMENT.getType(),XbbRefTypeEnum.CLUE.getCode(),XbbRefTypeEnum.CLUE.getAlias(),"enum.xbbRefType.name.clue", DistributorMarkEnum.OTHER.getCode()),
    /**
     * 临时额度
     */
    CREDIT_TEMPORARY_LIMIT(ProcessSaasAppEnum.FUND_MANAGEMENT.getType(), XbbRefTypeEnum.CREDIT_TEMPORARY_LIMIT.getCode(), XbbRefTypeEnum.CREDIT_TEMPORARY_LIMIT.getAlias(), "enum.xbbRefType.name.credit_temporary_limit", DistributorMarkEnum.DISTRIBUTOR.getCode()),
    /**
     * 固定额度
     */
    CREDIT_FIXED_LIMIT(ProcessSaasAppEnum.FUND_MANAGEMENT.getType(), XbbRefTypeEnum.CREDIT_FIXED_LIMIT.getCode(), XbbRefTypeEnum.CREDIT_FIXED_LIMIT.getAlias(), "enum.xbbRefType.name.credit_fixed_limit", DistributorMarkEnum.DISTRIBUTOR.getCode()),
    /**
     * 经销商
     */
    DISTRIBUTOR_MANAGEMENT(ProcessSaasAppEnum.DISTRIBUTOR.getType(), XbbRefTypeEnum.DISTRIBUTOR_MANAGEMENT.getCode(), XbbRefTypeEnum.DISTRIBUTOR_MANAGEMENT.getAlias(), "enum.xbbRefType.name.distributor_management", DistributorMarkEnum.DISTRIBUTOR.getCode()),
    /**
     * 经销商联系人
     */
    DISTRIBUTOR_CONTACT(ProcessSaasAppEnum.DISTRIBUTOR.getType(), XbbRefTypeEnum.DISTRIBUTOR_CONTACT.getCode(), XbbRefTypeEnum.DISTRIBUTOR_CONTACT.getAlias(), "enum.xbbRefType.name.distributor_contact", DistributorMarkEnum.DISTRIBUTOR.getCode()),
    /**
     * 订单
     */
    ORDER(ProcessSaasAppEnum.ORDER_CENTER.getType(), XbbRefTypeEnum.ORDER.getCode(), XbbRefTypeEnum.ORDER.getAlias(), "enum.xbbRefType.name.order", DistributorMarkEnum.DISTRIBUTOR.getCode()),
    /**
     * 退货单
     */
    RETURN(ProcessSaasAppEnum.ORDER_CENTER.getType(), XbbRefTypeEnum.RETURN.getCode(), XbbRefTypeEnum.RETURN.getAlias(), "enum.xbbRefType.name.return", DistributorMarkEnum.DISTRIBUTOR.getCode()),
    /**
     * 其他收入
     */
    OTHER_INCOME(ProcessSaasAppEnum.FUND_MANAGEMENT.getType(), XbbRefTypeEnum.OTHER_INCOME.getCode(), XbbRefTypeEnum.OTHER_INCOME.getAlias(), XbbRefTypeEnum.OTHER_INCOME.getI18nName(), DistributorMarkEnum.OTHER.getCode()),
    /**
     * 其他支出
     */
    OTHER_EXPENSE(ProcessSaasAppEnum.FUND_MANAGEMENT.getType(), XbbRefTypeEnum.OTHER_EXPENSE.getCode(), XbbRefTypeEnum.OTHER_EXPENSE.getAlias(), XbbRefTypeEnum.OTHER_EXPENSE.getI18nName(), DistributorMarkEnum.OTHER.getCode()),
    /**
     * 资金调拨单
     */
    FUND_TRANSFER(ProcessSaasAppEnum.FUND_MANAGEMENT.getType(), XbbRefTypeEnum.FUND_TRANSFER.getCode(), XbbRefTypeEnum.FUND_TRANSFER.getAlias(), XbbRefTypeEnum.FUND_TRANSFER.getI18nName(), DistributorMarkEnum.OTHER.getCode()),

    /**
     * 成本调整单
     */
    COST_ADJUST(ProcessSaasAppEnum.JXC.getType(), XbbRefTypeEnum.COST_ADJUST.getCode(), XbbRefTypeEnum.COST_ADJUST.getAlias(), XbbRefTypeEnum.COST_ADJUST.getI18nName(), DistributorMarkEnum.OTHER.getCode()),

    // 竞争对手
    COMPETITOR(ProcessSaasAppEnum.CRM.getType(),XbbRefTypeEnum.COMPETITOR.getCode(),XbbRefTypeEnum.COMPETITOR.getAlias(),XbbRefTypeEnum.COMPETITOR.getI18nName(), DistributorMarkEnum.OTHER.getCode()),

    /**
     * 工单v2
     */
    WORK_ORDER_V2(ProcessSaasAppEnum.WORK_ORDER_V2_CENTER.getType(),XbbRefTypeEnum.WORK_ORDER_V2.getCode(),XbbRefTypeEnum.WORK_ORDER_V2.getAlias(),XbbRefTypeEnum.WORK_ORDER_V2.getI18nName(), DistributorMarkEnum.OTHER.getCode()),
    /**
     * 回执单
     */
    RECEIPT_ORDER(ProcessSaasAppEnum.WORK_ORDER_V2_CENTER.getType(),XbbRefTypeEnum.RECEIPT_ORDER.getCode(),XbbRefTypeEnum.RECEIPT_ORDER.getAlias(),XbbRefTypeEnum.RECEIPT_ORDER.getI18nName(), DistributorMarkEnum.OTHER.getCode()),

    ;

    public Integer getDistributorMark() {
        return distributorMark;
    }

    public void setDistributorMark(Integer distributorMark) {
        this.distributorMark = distributorMark;
    }

    private Integer appCode;

    private Integer businessType;

    private String alias;

    private String memo;

    private Integer distributorMark;

    ProcessSaasFormEnum(Integer appCode, Integer businessType, String alias, String memo, Integer distributorMark) {
        this.appCode = appCode;
        this.businessType = businessType;
        this.alias = alias;
        this.memo = memo;
        this.distributorMark = distributorMark;
    }

    public Integer getAppCode() {
        return appCode;
    }

    public void setAppCode(Integer appCode) {
        this.appCode = appCode;
    }

    public Integer getBusinessType() {
        return businessType;
    }

    public void setBusinessType(Integer businessType) {
        this.businessType = businessType;
    }

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    public String getMemo() {
        return I18nMessageUtil.getMessage(memo);
    }

    public void setMemo(String memo) {
        this.memo = memo;
    }

    public static List<ProcessSaasFormEnum> getFormEnumList(Integer appCode) {
        List<ProcessSaasFormEnum> list = new ArrayList<>();
        for (ProcessSaasFormEnum formEnum : ProcessSaasFormEnum.values()) {
            if (Objects.equals(appCode, formEnum.getAppCode())) {
                list.add(formEnum);
            }
        }
        return list;
    }

    public static List<Integer> getBusinessTypeList(Integer appCode) {
        List<Integer> list = new ArrayList<>();
        for (ProcessSaasFormEnum formEnum : ProcessSaasFormEnum.values()) {
            if (Objects.equals(appCode, formEnum.getAppCode())) {
                list.add(formEnum.getBusinessType());
            }
        }
        return list;
    }

    /**
     * 是否包含此业务类型
     * @param businessType
     * @return
     */
    public static boolean containsBusinessType(Integer businessType) {
        List<Integer> list = Arrays.asList(XbbRefTypeEnum.WORK_ORDER_V2.getCode(), XbbRefTypeEnum.RECEIPT_ORDER.getCode(),XbbRefTypeEnum.SERVICE_PROJECT.getCode());
        if (list.contains(businessType)) {
            return false;
        }
        for (ProcessSaasFormEnum formEnum : ProcessSaasFormEnum.values()) {
            if (Objects.equals(businessType, formEnum.getBusinessType())) {
                return true;
            }
        }
        return false;
    }


    /**
     * 是否包含此业务类型
     * @param businessType
     * @return
     */
    public static boolean workflowApproveNode(Integer businessType) {
        List<Integer> list = Arrays.asList(XbbRefTypeEnum.WORK_ORDER_V2.getCode(), XbbRefTypeEnum.RECEIPT_ORDER.getCode(),XbbRefTypeEnum.SERVICE_PROJECT.getCode());
        if (list.contains(businessType)) {
            return true;
        }

        return containsBusinessType(businessType);
    }


    /**
     *
     * @param alias
     * @return
     */
    public static ProcessSaasFormEnum getByAlias(String alias) {
        List<String> list = Arrays.asList(XbbRefTypeEnum.WORK_ORDER_V2.getAlias(), XbbRefTypeEnum.RECEIPT_ORDER.getAlias(),XbbRefTypeEnum.SERVICE_PROJECT.getAlias());
        if (list.contains(alias)) {
            return null;
        }
        for (ProcessSaasFormEnum formEnum : ProcessSaasFormEnum.values()) {
            if (Objects.equals(alias, formEnum.getAlias())) {
                return formEnum;
            }
        }
        return null;
    }

    /**
     *
     * @param businessType
     * @return
     */
    public static ProcessSaasFormEnum getByBusinessType(Integer businessType) {
        for (ProcessSaasFormEnum formEnum : ProcessSaasFormEnum.values()) {
            if (Objects.equals(businessType, formEnum.getBusinessType())) {
                return formEnum;
            }
        }
        return null;
    }

    /**
     *
     * @param processSaasFormEnum
     * @return
     */
    public static ProcessSaasFormEnum transfer2Saas(ProcessSaasFormEnum processSaasFormEnum) {
        if (Objects.equals(processSaasFormEnum, ProcessSaasFormEnum.DISTRIBUTOR_MANAGEMENT)) {
            return ProcessSaasFormEnum.CUSTOMER;
        }
        if (Objects.equals(processSaasFormEnum, ProcessSaasFormEnum.DISTRIBUTOR_CONTACT)) {
            return ProcessSaasFormEnum.CONTACT;
        }
        if (Objects.equals(processSaasFormEnum, ProcessSaasFormEnum.ORDER)) {
            return ProcessSaasFormEnum.CONTRACT;
        }
        if (Objects.equals(processSaasFormEnum, ProcessSaasFormEnum.RETURN)) {
            return ProcessSaasFormEnum.REFUND;
        }
        return processSaasFormEnum;
    }
}
